import redis
import time
import random
import json
import re
import requests
from crawler_baidu.logger import LOG
from redis_con.redis_db import Get_Pool

get_pool = Get_Pool(host="localhost", port="6379",
                    max_connections=20)  # 实例化连接池对象
pool = get_pool.get_pool(db=0)  # 获得redis连接池


def get_proxy():
    """
    调用代理ip接口,获取代理ip值,并且将这些代理ip值和京东码进行关联存储
    :return: 返回代理ip的值
    """
    while True:
        try:
            # 代理ip url
            url = "http://gec.ip3366.net/api/?key=*****************&getnum=1&anonymoustype=3&filter=1&area=1&formats=2"
            resp = requests.get(url=url, timeout=5)
            data_json = resp.text
            ip = re.findall(r'"Ip":"(.*?)",', data_json)[0]  # 获取到的ip
            port = re.findall(r'"Port":(.*?),', data_json)[0]  # 获取到的端口号
            proxy = {'http': ip + ':' + port, }
        except:
            time.sleep(10)
            LOG.info('调用代理ip错误')
            continue
        break
    LOG.info('%s' % (proxy))
    return proxy


def set_proxy(expire_time=90):
    """
    获取代理ip,设置存活时间进入ip池,默认代理ip存活时间是90秒
    :param expire_time: 默认的存活时间
    :return: 无返回值
    """
    for i in range(1000000000):
        proxy = get_proxy()
        proxy_ip = json.dumps(proxy)  # 将字典值转成字符串
        with redis.Redis(connection_pool=pool) as con:
            while True:
                id = 'proxy_' + str(random.randint(1000, 9999))
                redis_key_list = con.keys('proxy_*')
                if not id in redis_key_list:
                    con.set(id, proxy_ip)
                    con.expire(id, expire_time)
                    break
                else:
                    pass
        time.sleep(30)


def my_get_proxy():
    """
    从代理ip池中提取存活得代理ip值
    :return: 返回存货的代理ip
    """
    with redis.Redis(connection_pool=pool) as con:
        while True:
            redis_key_list = con.keys('proxy_*')
            LOG.info('ip池剩余%s' % (redis_key_list))
            if redis_key_list:
                ip_id = random.choice(redis_key_list)
                ip = str(con.get(ip_id)).split('b\'')[1].split('\'')[0]
                ip_dict = json.loads(ip)  # 直接转成字典类型
                break
            else:
                LOG.info("ip池为空")
                time.sleep(10)
    LOG.info('%s' % (ip_dict))
    return ip_dict


if __name__ == '__main__':
    set_proxy()
